<?php

class Diner_model extends CI_Model
{
	public function __construct()
	{
		$this->load->database();
		$this->load->helper('date');
	}

	public function insert_diner($username, $password, $phone_number)
	{
		$data = array(
			'uname' => $username,
			'passwd' => substr(hash("sha256", $password),32),
			'phone_number' => $phone_number
		);
		if ($this->test_uniq('diner', 'uname', $username)){
			if ($this->test_uniq('diner', 'phone_number', $phone_number)){
				$this->db->insert('diner', $data);
				return 1;
			}else{
				return -1;
			}
		}else{
			return -2;
		}
	}

	public function test_uniq($table, $column, $data)
	{
		$result = $this->db->get_where($table, array($column => $data))->result();
		if(empty($result)){
			return true;
		}else{
			return false;
		}
	}

	public function get_merchant($offset)
	{
		$result = $this->db->query("SELECT id, mname, star FROM merchant LIMIT 20 OFFSET $offset")->result();
		if($result){
			return $result;
		}else{
			return false;
		}
	}

	public function get_mname_by_mid($id){
		$result = $this->db->query("SELECT mname FROM merchant WHERE id = $id")->row();
		if($result){
			return $result;
		}else{
			return false;
		}
	}

	public function getDishByDid($id)
	{
		$result = $this->db->query("SELECT id, dname, price, discount FROM dish WHERE id = $id")->result();
		if ($result){
			return $result;
		}else{
			return false;
		}
	}

	public function getDishesByMid($mid)
	{
		$result = $this->db->query("SELECT id, dname, price, discount FROM dish WHERE mid = $mid")->result_array();
		if($result){
			$result = self::indexArrayToRelatedArray($result, 'id');
			return $result;
		}else{
			return false;
		}
	}

	public static function indexArrayToRelatedArray(array $array, $indexName)
	{
		$data = array();
		foreach ($array as $key => $value) {
			if (!is_numeric($key)) {
				continue;
			}

			$index = '';
			if (is_array($value) && isset($value[$indexName]) && !empty($value[$indexName])) {
				$index = $value[$indexName];
			} elseif (is_object($value) && isset($value->$indexName) && !empty($value->$indexName)) {
				$index = $value->$indexName;
			}

			if (!empty($index)) {
				$data[$index] = $value;
			}

		}
		return $data;
	}

	public function getDishesByOrder($order)
	{
		if (empty($order)){
			return false;
		}
		$result = array();
		foreach ($order as $id => $number){
			$set = $this->db->query("SELECT * FROM dish WHERE id = $id")->row_array();
			$set['number'] = $number['number'];
			$result[] = $set;
		}
		if($result){
			return $result;
		}else{
			return false;
		}
	}

	public function get_addr_by_did($id)
	{
		$result = $this->db->query("SELECT * FROM address WHERE did = $id")->result();
		if($result){
			return $result;
		}else{
			return false;
		}
	}

	public function submit_order_from_session($addrid, $mid)
	{
		$order = $this->getDishesByOrder($_SESSION['order'][$mid]);
		$money = 0;
		foreach ($order as $dish){
			$money += $dish['price'] * $dish['discount'] * $dish['number'];
		}
		$data = array(
			'comfirm_time' => date('Y-m-d H:i:s', now('Asia/Shanghai')),
			'money' => $money,
			'addrid' => $addrid,
			'mid' => $mid
		);
		$this->db->insert('orders', $data);
		$orderid = $this->db->insert_id();
		foreach ($order as $dish){
			$this->db->insert('component', array("orderid"=>$orderid, "dishid"=>$dish['id'], "number"=>$dish['number']));
		}
		if ($this->db->affected_rows()){
			return true;
		}else{
			return false;
		}
	}

	public function add_addr($addr, $rname, $phone_number, $did){
		$data = array(
			"addr" => $addr,
			"rname" => $rname,
			"phone_number" => $phone_number,
			"did" => $did
		);
		$result = $this->db->insert("address", $data);
		return $result;
	}

	public function get_my_received_order($id)
	{
		$sql = "SELECT comfirm_time, arrival_time, address.addr dst_addr, address.rname dst_name, address.phone_number dst_phone, 
				merchant.mname src_name, merchant.address src_addr, merchant.phone_number src_phone,
				rider.id rid, rider.phone_number rider_phone
				FROM orders, address, merchant, rider
				WHERE address.id IN (SELECT id FROM address WHERE did = $id) AND 
				orders.mid = merchant.id AND address.id = orders.addrid AND rider.id = orders.rid
				ORDER BY 1, 2 DESC";
		$result = $this->db->query($sql)->result();
		if($result){
			return $result;
		}else{
			return false;
		}
	}

	public function get_my_unreceived_order($id)
	{
		$sql = "SELECT comfirm_time, address.addr dst_addr, address.rname dst_name, address.phone_number dst_phone, 
				merchant.mname src_name, merchant.address src_addr, merchant.phone_number src_phone
				FROM orders, address, merchant
				WHERE address.id IN (SELECT id FROM address WHERE did = $id) AND 
				orders.mid = merchant.id AND address.id = orders.addrid AND orders.rid IS NULL
				ORDER BY 1 DESC";
		$result = $this->db->query($sql)->result();
		if($result){
			return $result;
		}else{
			return false;
		}
	}
}
